package com.cqjtu.controller;

import com.cqjtu.pojo.PageBean;
import com.cqjtu.pojo.Result;
import com.cqjtu.pojo.Teams;
import com.cqjtu.pojo.Worker;
import com.cqjtu.service.TeamsService;
import com.cqjtu.service.WorkerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
public class WorkerController {

    @Autowired
    private WorkerService workerService;
    //删除工人
    @DeleteMapping("/worker/{workerId}")
    public Result delete(@PathVariable List<Integer> workerId){
        log.info("删除操作，ids:{}",workerId);
        workerService.delete(workerId);
        return Result.success();
    }

    //新增工人
    @Autowired
    private TeamsService teamsService;
    @PostMapping("/workerInsert")
    public Result add(@RequestBody Worker worker){
        log.info("新增员工，worker:{}",worker);
        workerService.add(worker);
        teamsService.add2(worker);
        return Result.success();
    }

    //工人条件查询(通过工人姓名和班组)
    @GetMapping("/workerInfoByNameTeamsId")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String workerName,String teamsName){
        log.info("分页查询，参数：{},{},{},{}",page,pageSize,workerName,teamsName);
        //调用service分页查询
        PageBean pageBean = workerService.page(page,pageSize,workerName,teamsName);
        return Result.success(pageBean);
    }

    //工人查询(通过id)
    @GetMapping("/workerInfo/{workerId}")
    public Result find(@PathVariable Integer workerId){
        log.info("工人查询,id为:{}",workerId);

        return Result.success(workerService.find(workerId));
    }

    //更新工人
    @PutMapping("/workerInfoUpdate")
    public Result updateWorker(@RequestBody Worker worker){
        workerService.updateWorker(worker);
        return Result.success();
    }

    //工人条件查询(班组id)
    @GetMapping("/ByteamsId/{teamsId}")
    public Result page2(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                    @PathVariable  Integer teamsId){
        log.info("分页查询，参数：{},{},{}",page,pageSize,teamsId);
        //调用service分页查询
        PageBean pageBean = workerService.page2(page,pageSize,teamsId);
        return Result.success(pageBean);
    }


    //班组查询（查询组名）
    @GetMapping("/workerInfo")
    public Result page3(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                        String workerName,String teamsName){
        log.info("分页查询，参数：{},{},{},{}",page,pageSize,workerName,teamsName);
        //调用service分页查询
        PageBean pageBean = workerService.page3(page,pageSize,workerName,teamsName);
        return Result.success(pageBean);
    }

}
